
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>说说你理解的ConcurrentHashMap - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>说说你理解的ConcurrentHashMap - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">理论</div>
          <div class="card-question">Java中的`ConcurrentHashMap`相比于`HashMap`，主要解决了什么问题？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">理论</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`ConcurrentHashMap`是为并发编程设计的高效线程安全的哈希表。它解决了在多线程环境下使用`HashMap`可能引发的数据不一致或线程冲突问题，同时避免了传统同步机制（如对整个Map加锁）可能带来的性能瓶颈。</div>
          </div>
          <div class="card-source">来源: 文档开篇第一段及「4. 高效的并发性」</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">在JDK 7及之前版本中，`ConcurrentHashMap`是如何通过“分段锁”机制实现高并发性的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">它将整个哈希表分割成多个`Segment`，每个`Segment`都拥有自己独立的锁。当一个线程需要操作数据时，它只需锁定目标数据所在的`Segment`，而不是整个`ConcurrentHashMap`。这使得多个线程可以同时访问不同的`Segment`，从而减少了锁竞争，提高了并发性。</div>
          </div>
          <div class="card-source">来源: 2. 分段锁机制（JDK 7 及之前）</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">从JDK 8开始，`ConcurrentHashMap`的线程安全实现机制发生了哪些核心变化？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">JDK 8摒弃了分段锁，转而采用粒度更细的“CAS（Compare-And-Swap）操作”和分桶（Bucket）机制。插入和更新等操作主要通过CAS这种乐观锁机制保证线程安全，锁的粒度细化到了每个桶的头节点，从而实现了更高效的并发。</div>
          </div>
          <div class="card-source">来源: 3. CAS 操作和分桶（JDK 8 及之后）</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">文档中提到的`ConcurrentHashMap`的“弱一致性”体现在哪里？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`ConcurrentHashMap`的弱一致性体现在读取操作上。读取操作可能在没有获取锁的情况下进行，这意味着读取线程可能会看到最新的数据，也可能看到某个线程修改之前的值。换言之，读取操作不会因为写入操作而被阻塞。</div>
          </div>
          <div class="card-source">来源: 5. 弱一致性</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">与`HashMap`类似，JDK 8的`ConcurrentHashMap`在什么情况下会将链表转换为红黑树？这样做的目的是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当某个桶（bucket）中的链表长度超过一个特定的阈值时，该链表会被转换为红黑树。这样做的目的是为了在哈希冲突严重的情况下，依然能够提高查找和插入操作的效率。</div>
          </div>
          <div class="card-source">来源: 3. CAS 操作和分桶（JDK 8 及之后）</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">应用</div>
          <div class="card-question">根据文档，请列举`ConcurrentHashMap`的三个典型应用场景。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">应用</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">1. 缓存实现：在需要高并发访问的缓存系统中使用。<br>2. 计数器：当多个线程需要共享和更新一个计数器时使用。<br>3. 分布式系统：作为处理大量并发请求的分布式系统中的数据共享基础设施。</div>
          </div>
          <div class="card-source">来源: 8. 典型应用场景</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">JDK 8为`ConcurrentHashMap`引入的批量操作（如forEach, reduce）是如何实现高效并行处理的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">JDK 8引入的批量操作方法内部利用了`ForkJoinPool`来并行处理数据。这使得在处理大规模数据时，能够充分利用多核处理器的能力，从而大大提高了处理效率。</div>
          </div>
          <div class="card-source">来源: 6. 批量操作</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">在JDK 7的`ConcurrentHashMap`实现中，读操作是否需要加锁？为什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">在大多数情况下，读操作是无锁的。因为读取操作不会修改哈希表的结构，所以允许多个线程同时进行读取而无需加锁，从而提高了性能。</div>
          </div>
          <div class="card-source">来源: 2. 分段锁机制（JDK 7 及之前）</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">相较于使用`Collections.synchronizedMap`包装的Map，`ConcurrentHashMap`在并发性能上有什么优势？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`Collections.synchronizedMap`通过对整个Map对象加锁来实现线程安全，导致任何时候只有一个线程能访问Map。而`ConcurrentHashMap`通过分段锁（JDK 7）或CAS操作（JDK 8）等更精细的锁机制，大大减少了锁的粒度和竞争，因此在高并发场景下具有更好的性能和扩展性。</div>
          </div>
          <div class="card-source">来源: 4. 高效的并发性</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
